<?php
namespace Home\Controller;

class ItController extends CommonAction
{
    private $_caseType = array(
        'ecs' => 'ECS服务器',
        'mysql' => 'MySQL实例',
        'nginx' => 'Nginx实例',
        'redis' => 'Redis实例',
        'tomcat' => 'Tomcat应用',
        'domainname' => '二级域名'
    );

    public function ItController()
    {
        parent::__construct();
    }

    protected function _getNavigator($name, $action)
    {
        $navigator = array(
            array(
                'name' => '运维信息',
                'url' => '###'
            ),
            array(
                'name' => $name,
                'url' => $action
            )
        );
        return $navigator;
    }

    public function index()
    {
        $dept = $_SESSION['dept'];
        $userId = $_SESSION['user_id'];
        $dept = D('Dept', 'Service')->getSpecialUserDept($userId, $dept);

        $caseInfo = array();
        foreach ($this->_caseType as $key => $value) {
            $sql = 'select count(*) as ' . $key . ' from `zt_itoa_' . $key . '_info` where `is_deleted` = "N"';
            $res = M()->query($sql);
            $caseInfo[$key] = array(
                'name' => $value,
                'amount' => $res['0'][$key]
            );
        }
        $this->assign('caseInfo', $caseInfo);
        $sectionId = $this->_getSectionId(__CLASS__, __FUNCTION__);
        $this->assign('navigator', $this->_getNavigator('服务总览', '/home/' . $sectionId));
        $this->assign('section', $sectionId);
        $this->display();
    }

    public function ecs()
    {
        $dept = $_SESSION['dept'];
        if ($dept == 19) {
            $this->assign('isIt', true);
        } else {
            $this->assign('isIt', false);
        }
        $ecsList = D('It', 'Service')->ecs();
        $this->assign('ecsList', $ecsList);
        $sectionId = $this->_getSectionId(__CLASS__, __FUNCTION__);
        $this->assign('navigator', $this->_getNavigator('ECS服务器', '/home/' . $sectionId));
        $this->assign('section', $sectionId);
        $this->display();
    }

    public function mysql()
    {
        $mysqlList = D('It', 'Service')->mysql();
        $this->assign('mysqlList', $mysqlList);
        $sectionId = $this->_getSectionId(__CLASS__, __FUNCTION__);
        $this->assign('navigator', $this->_getNavigator('MySQL实例', '/home/' . $sectionId));
        $this->assign('section', $sectionId);
        $this->display();
    }

    public function nginx()
    {
        $nginxList = D('It', 'Service')->nginx();
        $this->assign('nginxList', $nginxList);
        $sectionId = $this->_getSectionId(__CLASS__, __FUNCTION__);
        $this->assign('navigator', $this->_getNavigator('Nginx实例', '/home/' . $sectionId));
        $this->assign('section', $sectionId);
        $this->display();
    }

    public function tomcat()
    {
        $tomcatList = D('It', 'Service')->tomcat();
        $this->assign('tomcatList', $tomcatList);
        $sectionId = $this->_getSectionId(__CLASS__, __FUNCTION__);
        $this->assign('navigator', $this->_getNavigator('Tomcat应用', '/home/' . $sectionId));
        $this->assign('section', $sectionId);
        $this->display();
    }

    public function redis()
    {
        $redisList = D('It', 'Service')->redis();
        $this->assign('redisList', $redisList);
        $sectionId = $this->_getSectionId(__CLASS__, __FUNCTION__);
        $this->assign('navigator', $this->_getNavigator('Redis实例', '/home/' . $sectionId));
        $this->assign('section', $sectionId);
        $this->display();
    }

    public function ecs_detail($id = null)
    {
        if ($id == null) {
            $this->error("请输入ecs的id！");
        }
        $ecsList = D('It', 'Service')->ecs($id);
        $mysqlList = D('It', 'Service')->mysql($id);
        $nginxList = D('It', 'Service')->nginx($id);
        $tomcatList = D('It', 'Service')->tomcat($id);
        $redisList = D('It', 'Service')->redis($id);

        $this->assign('ecsList', $ecsList);
        $this->assign('mysqlList', $mysqlList);
        $this->assign('nginxList', $nginxList);
        $this->assign('tomcatList', $tomcatList);
        $this->assign('redisList', $redisList);

        $sectionId = $this->_getSectionId(__CLASS__, __FUNCTION__);
        $this->assign('navigator',
            array(
                array(
                    'name' => '运维信息',
                    'url' => '###'
                ),
                array(
                    'name' => 'ECS服务器',
                    'url' => '/home/it/ecs'
                ),
                array(
                    'name' => 'ECS环境预览',
                    'url' => $sectionId
                )
            )
        );
        $this->assign('section', $sectionId);
        $this->display();
    }

    public function get_ecs()
    {
        $ecsList = D('It', 'Service')->ecs();
        $res = array(
            'errorCode' => '0',
            'errorMsg' => 'success',
            'data' => $ecsList
        );
        $this->ajaxReturn($res);
    }

    public function get_mysql_master_list()
    {
        $sql = "SELECT
            zimi.id,
            hostname,
            inst_role
        FROM
            zt_itoa_mysql_info AS zimi
        LEFT JOIN zt_itoa_ecs_info AS ziei ON zimi.ecs_id = ziei.id
        WHERE
            inst_role = 'Master'";
        $data = M()->query($sql);
        $res = array(
            'errorCode' => '0',
            'errorMsg' => 'success',
            'data' => $data
        );
        $this->ajaxReturn($res);
    }

    public function save_ecs()
    {
        $id = $this->_getParam('id', 0);
        $hostname = $this->_getParam('hostname', '');
        $innerIp = $this->_getParam('inner_ip', '');
        $outerIp = $this->_getParam('outer_ip', '');
        $config = $this->_getParam('config', '');
        $location = $this->_getParam('location', '');
        $environment = $this->_getParam('environment', '');
        $os = $this->_getParam('os', '');
        $port = $this->_getParam('port', '');
        $usedFor = $this->_getParam('used_for', '');
        $users = $this->_getParam('users', array());
        if (empty($users)) {
            $res = array(
                'errorCode' => '-1',
                'errorMsg' => '请选择负责人！'
            );
            $this->ajaxReturn($res);
        }
        if ($id == 0) {
            // 新增一条记录
            $data = array(
                'hostname' => $hostname,
                'gmt_create' => date('c'),
                'gmt_modified' => date('c'),
                'extend_desc' => $usedFor,
                'env_type' => $environment,
                'eth0' => $innerIp,
                'eth1' => $outerIp,
                'configuration' => $config,
                'position' => $location,
                'os' => $os,
                'ssh_port' => $port
            );
            $appId = M('ItoaEcsInfo')->add($data);

            foreach ($users as $user) {
                $userData = array(
                    'gmt_create' => date('c'),
                    'gmt_modified' => date('c'),
                    'user_id' => $user,
                    'app_type' => 'ecs',
                    'app_id' => $appId
                );
                M('ItoaUserAdmin')->add($userData);
            }
        } else {
            $data = array(
                'hostname' => $hostname,
                'gmt_modified' => date('c'),
                'extend_desc' => $usedFor,
                'env_type' => $environment,
                'eth0' => $innerIp,
                'eth1' => $outerIp,
                'configuration' => $config,
                'position' => $location,
                'os' => $os,
                'ssh_port' => $port
            );
            // 更新一条记录
            $res = M('ItoaEcsInfo')->where('id=' . $id)->save($data);

            $userAdminList = M('ItoaUserAdmin')->where('app_type = "ecs" and app_id = ' . $id)->select();
            $userAdminArr = array();
            foreach ($userAdminList as $userAdmin) {
                $userAdminArr[] = $userAdmin['user_id'];
            }
            // 删除管理员
            $needDeleteArr = array_diff($userAdminArr, $users);
            foreach ($needDeleteArr as $needDelete) {
                $data = array(
                    'is_deleted' => 'Y'
                );
                // 更新一条记录
                $res = M('ItoaUserAdmin')->where('id=' . $needDelete)->save($data);
            }
            // 新增管理员
            $needAddArr = array_diff($users, $userAdminArr);
            foreach ($needAddArr as $needAdd) {
                $userData = array(
                    'gmt_create' => date('c'),
                    'gmt_modified' => date('c'),
                    'user_id' => $needAdd,
                    'app_type' => 'ecs',
                    'app_id' => $id
                );
                M('ItoaUserAdmin')->add($userData);
            }
        }
        $res = array(
            'errorCode' => '0',
            'errorMsg' => '保存成功！'
        );
        $this->ajaxReturn($res);
    }

    public function save_mysql()
    {
        $id = $this->_getParam('id', 0);
        $ecs_id = $this->_getParam('ecs_id', '');
        $port = $this->_getParam('port', '');
        $env_type = $this->_getParam('env_type', '');
        $inst_role = $this->_getParam('inst_role', '');
        $master_id = $this->_getParam('master_id', '');
        $extend_desc = $this->_getParam('extend_desc', '');
        $version = $this->_getParam('version', '');
        $datadir = $this->_getParam('datadir', '');
        $basedir = $this->_getParam('basedir', '');
        $conf_path = $this->_getParam('conf_path', array());
        $command = $this->_getParam('command', array());

        if ($id == 0) {
            // 新增一条记录
            $data = array(
                'gmt_create' => date('c'),
                'gmt_modified' => date('c'),
                'extend_desc' => $extend_desc,
                'ecs_id' => $ecs_id,
                'env_type' => $env_type,
                'port' => $port,
                'inst_role' => $inst_role,
                'master_id' => $master_id,
                'version' => $version,
                'basedir' => $basedir,
                'datadir' => $datadir,
                'conf_path' => $conf_path,
                'command' => $command
            );
            $res = M('ItoaMysqlInfo')->add($data);
        } else {
            $data = array(
                'gmt_modified' => date('c'),
                'extend_desc' => $extend_desc,
                'ecs_id' => $ecs_id,
                'env_type' => $env_type,
                'port' => $port,
                'inst_role' => $inst_role,
                'master_id' => $master_id,
                'version' => $version,
                'basedir' => $basedir,
                'datadir' => $datadir,
                'conf_path' => $conf_path,
                'command' => $command
            );
            // 更新一条记录
            $res = M('ItoaMysqlInfo')->where('id=' . $id)->save($data);
        }
        if (empty($res)) {
            $ret = array(
                'errorCode' => '-1',
                'errorMsg' => '保存失败！'
            );
        } else {
            $ret = array(
                'errorCode' => '0',
                'errorMsg' => '保存成功！'
            );
        }

        $this->ajaxReturn($ret);
    }

    public function save_nginx()
    {
        $id = $this->_getParam('id', 0);
        $ecs_id = $this->_getParam('ecs_id', '');
        $port = $this->_getParam('port', '');
        $env_type = $this->_getParam('env_type', '');
        $version = $this->_getParam('version', '');
        $config = $this->_getParam('config', '');
        $log = $this->_getParam('log', '');
        $command = $this->_getParam('command', array());

        if ($id == 0) {
            // 新增一条记录
            $data = array(
                'gmt_create' => date('c'),
                'gmt_modified' => date('c'),
                'ecs_id' => $ecs_id,
                'env_type' => $env_type,
                'port' => $port,
                'version' => $version,
                'conf_path' => $config,
                'logfile_path' => $log,
                'command' => $command
            );
            $res = M('ItoaNginxInfo')->add($data);
        } else {
            $data = array(
                'gmt_modified' => date('c'),
                'ecs_id' => $ecs_id,
                'env_type' => $env_type,
                'port' => $port,
                'version' => $version,
                'conf_path' => $config,
                'logfile_path' => $log,
                'command' => $command
            );
            // 更新一条记录
            $res = M('ItoaNginxInfo')->where('id=' . $id)->save($data);
        }
        if (empty($res)) {
            $ret = array(
                'errorCode' => '-1',
                'errorMsg' => '保存失败！'
            );
        } else {
            $ret = array(
                'errorCode' => '0',
                'errorMsg' => '保存成功！'
            );
        }

        $this->ajaxReturn($ret);
    }
}
